/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.coraci.anag.gui.components.basic.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.File;
import javax.swing.GroupLayout;
import javax.swing.JFileChooser;
import javax.swing.WindowConstants;

/**
 *
 * @author Luca
 */
public class AnagDBFileChooserDialog extends javax.swing.JDialog {
    
    private PropertyChangeSupport pps = new PropertyChangeSupport(this);
    
    public void addPropertySupportListener(PropertyChangeListener p) {
        pps.addPropertyChangeListener(p);
    }
    
    public void removePropertySupportListener(PropertyChangeListener p) {
        pps.removePropertyChangeListener(p);
    }

    /**
     * Creates new form DVPISFileChooserDialog
     */
    public AnagDBFileChooserDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
//        String certificate = I18nTranslator.getInstance().translate(LangKeywords.LOGIN_CERTIFICATE_FILTER);
//        this.fileChooser.addChoosableFileFilter(new ExtensionFileFilter(certificate, "jks"));
        this.fileChooser.addActionListener(new MyActionListener(this));
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        fileChooser = new JFileChooser();

        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addComponent(fileChooser, GroupLayout.Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 589, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addComponent(fileChooser, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public final JFileChooser getFileChooser() {
        return fileChooser;
    }

    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(AnagDBFileChooserDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(AnagDBFileChooserDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(AnagDBFileChooserDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(AnagDBFileChooserDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                AnagDBFileChooserDialog dialog = new AnagDBFileChooserDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private JFileChooser fileChooser;
    // End of variables declaration//GEN-END:variables

    
    class MyActionListener implements ActionListener {
        private AnagDBFileChooserDialog dialog;

        public MyActionListener(AnagDBFileChooserDialog dialog) {
            this.dialog = dialog;
        }
        
        
        
        @Override
            public void actionPerformed(ActionEvent e) {
                String actionCommand = e.getActionCommand();
                if(e.getActionCommand().equals(JFileChooser.APPROVE_SELECTION)){
                    File selectedFile = this.dialog.getFileChooser().getSelectedFile();
                    System.out.println("selected File = "+selectedFile);
                    pps.firePropertyChange(JFileChooser.APPROVE_SELECTION, null, selectedFile.getAbsoluteFile());
                    
                }else if(e.getActionCommand().equals(JFileChooser.CANCEL_SELECTION)){
                    this.dialog.setVisible(false);
                    this.dialog.dispose();
                }
                System.out.println("action command -> "+actionCommand);
            }
        }

}


